<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/5/28
 * Time: 16:02
 */


require __DIR__."/login_filter.php";
//require dirname(__DIR__)."/common.php";

use app\cls\tom\Config;
use app\cls\tom\Response;
use app\cls\upload\FileUploadException;
use app\cls\upload\SingleUpload;


if(empty($_REQUEST["upload_type"])) {
    Response::output(-1, "参数错误");
}
$redis = \app\cls\tom\SysResource::getRedis();
$allUploadConfig = Config::load("cos_upload");
if(!isset($allUploadConfig[$_REQUEST["upload_type"]])){
    Response::output(-1,"can't support the upload type {$_REQUEST["upload_type"]}!");
}
$uploadConfig = $allUploadConfig[$_REQUEST["upload_type"]];
if(empty($uploadConfig)){
    Response::output(-1,"不支持该类型",["error_field"=>"upload_type"]);
}
$uploadConfig["redis"] = $redis;
try{
    $singleUpload = new SingleUpload($uploadConfig);
    $singleUpload->doupload();
    $fileInfo = $singleUpload->getFileDataInfo();
    Response::output(0,"上传文件成功",
        array_merge($fileInfo, ["access_url"=>$fileInfo["file_path"]]));
}catch (FileUploadException $e){
    Response::output(-1,"上传文件失败:【msg===>{$e->getMessage()}】");
}
